//
//  NONavThreeViewController.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/6/21.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "NONavThreeViewController.h"

@implementation CityModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}

@end

@interface NONavThreeViewController ()

@property (nonatomic, strong) NSMutableArray *sectionList;

@end

@implementation NONavThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 50, 50);
    [button setTitle:@"OK" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.sectionList = [NSMutableArray arrayWithCapacity:10];
    NSArray *cityOriginNameList = @[@"北京",@"天津",@"内蒙古",@"呼和浩特",@"新疆",@"河南",@"洛阳",@"开封",@"郑州",@"商丘",@"乌鲁木齐",@"杭州",@"南京",@"深圳",@"广州",@"广东",@"伊利",@"石家庄",@"哈尔滨",@"长春",@"沈阳",@"四川",@"成都",@"汝州",@"吉林",@"丹东",@"北海"];
    NSMutableArray *cityOriginModelList = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i<cityOriginNameList.count; i++) {
        CityModel *model = [[CityModel alloc] init];
        model.cityName = cityOriginNameList[i];
        model.cityId = 100+i;
        [cityOriginModelList addObject:model];
    }
    NSArray *cityList = [self sortObjectsAccordingToInitialWith:cityOriginModelList];
    for (NSArray *itemArr in cityList) {
        NSLog(@"%@",self.sectionList[[cityList indexOfObject:itemArr]]);
        for (CityModel *cityModel in itemArr) {
            NSLog(@"cityModel:%ld -- %@",(long)cityModel.cityId,cityModel.cityName);
        }
    }
}

- (void)okAction:(UIButton *)sender
{
    UIViewController *vc = self.presentingViewController;
    [self dismissViewControllerAnimated:NO completion:^{
        [vc dismissViewControllerAnimated:NO completion:nil];
    }];
    
    dispatch_queue_t queue1 = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue2 = dispatch_get_main_queue();
    
    NSLog(@"之前 - %@", [NSThread currentThread]);
    dispatch_async(queue1, ^{
        NSLog(@"sync之前 - %@",[NSThread currentThread]);
        //dispatch_queue_t someQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
        dispatch_sync(queue2, ^{
            NSLog(@"sync - %@",[NSThread currentThread]);
        });
        NSLog(@"sync之后 - %@",[NSThread currentThread]);
    });
    NSLog(@"之后 - %@",[NSThread currentThread]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 按首字母分组排序数组
-(NSMutableArray *)sortObjectsAccordingToInitialWith:(NSArray *)arr {
    
    // 初始化UILocalizedIndexedCollation
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    
    //得出collation索引的数量，这里是27个（26个字母和1个#）
    NSInteger sectionTitlesCount = [[collation sectionTitles] count];
    //初始化一个数组newSectionsArray用来存放最终的数据，我们最终要得到的数据模型应该形如@[@[以A开头的数据数组], @[以B开头的数据数组], @[以C开头的数据数组], ... @[以#(其它)开头的数据数组]]
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    
    //初始化27个空数组加入newSectionsArray
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }
    
    //将每个名字分到某个section下
    for (CityModel *city in arr) {
        //获取name属性的值所在的位置，比如"林丹"，首字母是L，在A~Z中排第11（第一位是0），sectionNumber就为11
        NSInteger sectionNumber = [collation sectionForObject:city collationStringSelector:@selector(cityName)];
        //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
        NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
        [sectionNames addObject:city];
    }
    
    //对每个section中的数组按照name属性排序
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *cityArrayForSection = newSectionsArray[index];
        NSArray *sortedCityArrayForSection = [collation sortedArrayFromArray:cityArrayForSection collationStringSelector:@selector(cityName)];
        newSectionsArray[index] = sortedCityArrayForSection;
    }
    
    //删除空的数组
    NSMutableArray *finalArr = [NSMutableArray new];
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        if (((NSMutableArray *)(newSectionsArray[index])).count != 0) {
            [self.sectionList addObject:[collation sectionTitles][index]];
            [finalArr addObject:newSectionsArray[index]];
        }
    }
    return finalArr;
}

@end
